package com.his.controller;

import com.his.annotation.RequireRoles;
import com.his.common.Const;
import com.his.common.Rsp;
import com.his.common.Temp;
import com.his.entity.Order;
import com.his.service.OrderService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;

/**
 * (Order)表控制层
 *
 * @author makejava
 * @since 2020-10-23 18:13:59
 */
@Validated
@RestController
@RequestMapping("order")
public class OrderController {
    /**
     * 服务对象
     */
    @Resource
    private OrderService orderService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("selectOne")
    public Order selectOne(Integer id) {
        return this.orderService.queryById(id);
    }

    /**
     * 查询病人的订单,根据病人的id
     *
     * @param token        token令牌
     * @param patientPhone 病人电话
     * @return 返回订单数据
     */
    @RequireRoles(roles = {"hr", "charge", "admin"}, index = 0)
    @GetMapping("getByPatientPhone")
    public Rsp getByPatientId(@RequestHeader(Const.TOKEN) String token, @NotBlank String patientPhone) {
        return orderService.getByPatientPhone(patientPhone);
    }

    @RequireRoles(roles = {"hr", "charge"}, index = 0)
    @PostMapping("pay")
    public Rsp pay(@RequestHeader(Const.TOKEN) String token, @RequestBody Temp temp) {
        if (temp.getId().length == 0) {
            return Rsp.fail().msg("支付订单数量不能为零");
        }
        return orderService.payAll(temp.getId());
    }

    @RequireRoles(roles = {"hr", "charge"}, index = 0)
    @PutMapping("payApplication")
    public Rsp payApplication(@RequestHeader(Const.TOKEN) String token, @NotNull Integer id) {
        return orderService.payApplication(id);
    }

    @RequireRoles(roles = {"hr", "admin"}, index = 0)
    @PostMapping("getPayQRCode")
    public Rsp getPayQRCode(@RequestHeader(Const.TOKEN) String token
            , @RequestBody  Order pay) {
        return orderService.getPayQRCode(pay);
    }
    @RequireRoles(roles = {"hr", "admin"}, index = 0)
    @GetMapping("payQuery")
    public Rsp payQuery(@RequestHeader(Const.TOKEN) String token
            , @NotNull(message = "订单编号不能为空") String id) {
        return orderService.payQuery(id);
    }
}